﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Configuration;

namespace iLangl.Plugin.ConfigFileSettings
{
	public class PluginConfiguration : ConfigurationSection, IPluginConfiguration
	{
		private static ConfigurationProperty accountUserName;
		private static ConfigurationProperty accountPassword;
		private static ConfigurationProperty pluginLicenseKey;
		private static ConfigurationProperty siteIdKey;
		private static ConfigurationProperty publishUri;
		
		private static PluginConfiguration instance;
		private static ConfigurationPropertyCollection properties;

		static PluginConfiguration()
		{
			accountUserName = new ConfigurationProperty(
			  "accountUserName",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);

			accountPassword = new ConfigurationProperty(
			  "accountPassword",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);

			pluginLicenseKey = new ConfigurationProperty(
			  "pluginLicenseKey",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);

			siteIdKey = new ConfigurationProperty(
		  "siteIdKey",
		  typeof(String),
		  null,
		  ConfigurationPropertyOptions.None);

			publishUri = new ConfigurationProperty(
		  "publishUri",
		  typeof(String),
		  null,
		  ConfigurationPropertyOptions.None);

			properties = new ConfigurationPropertyCollection();
			properties.Add(accountUserName);
			properties.Add(accountPassword);
			properties.Add(pluginLicenseKey);
			properties.Add(siteIdKey);
			properties.Add(publishUri);
		}

		private const String SectionName = "PluginConfiguration";
		private static object mutex = new object();
		/// <summary>
		/// The instance of the PluginConfiguration.
		/// </summary>
		public static PluginConfiguration Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = System.Configuration.ConfigurationManager.GetSection(SectionName) as PluginConfiguration;
						}
					}
				}
				return instance;
			}
		}

		public void Save()
		{
			Configuration config = null;

			try
			{
				config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
			}
			catch
			{
				config = WebConfigurationManager.OpenWebConfiguration("/");
			}

			ConfigurationSection section = config.GetSection(SectionName);
			section.ElementInformation.Properties[accountUserName.Name].Value = AccountUserName;
			section.ElementInformation.Properties[accountPassword.Name].Value = AccountPassword;
			section.ElementInformation.Properties[pluginLicenseKey.Name].Value = PluginLicenseKey;
			section.ElementInformation.Properties[siteIdKey.Name].Value = PluginLicenseKey;
			section.ElementInformation.Properties[publishUri.Name].Value = PublishURI;
			config.Save(ConfigurationSaveMode.Modified);
			System.Configuration.ConfigurationManager.RefreshSection(SectionName);
		}

		private String _accountUserName = String.Empty;
		/// <summary>
		/// Account User Name
		/// </summary>
		public String AccountUserName
		{
			get
			{
				if (!String.IsNullOrEmpty(_accountUserName))
				{
					return _accountUserName;
				}

				_accountUserName = base[accountUserName] as String;

				return _accountUserName;
			}
		}

		public void SetAccountUserName(string accountUserName)
		{
			_accountUserName = accountUserName;
		}



		private String _accountPassword = String.Empty;
		/// <summary>
		/// Account Password
		/// </summary>
		public String AccountPassword
		{
			get
			{
				if (!String.IsNullOrEmpty(_accountPassword))
				{
					return _accountPassword;
				}

				_accountPassword = base[accountPassword] as String;

				return _accountPassword;
			}
		}

		public void SetAccountPassword(string accountPassword)
		{
			_accountPassword = accountPassword;
		}


		private String _pluginLicenseKey = String.Empty;
		/// <summary>
		/// Account Password
		/// </summary>
		public String PluginLicenseKey
		{
			get
			{
				if (!String.IsNullOrEmpty(_pluginLicenseKey))
				{
					return _pluginLicenseKey;
				}

				_pluginLicenseKey = base[pluginLicenseKey] as String;

				return _pluginLicenseKey;
			}
		}

		public void SetPluginLicenseKey(string pluginLicenseKey)
		{
			_pluginLicenseKey = pluginLicenseKey;
		}

		protected override ConfigurationPropertyCollection Properties
		{
			get
			{
				return properties;
			}
		}

		

		private string _siteId = String.Empty;
		public string SiteId
		{
			get
			{
				if (!String.IsNullOrEmpty(_siteId))
				{
					return _siteId;
				}

				_siteId = base[siteIdKey] as String;

				return _siteId;
			}
			set
			{
				_siteId = value;
			}
		}


		private string _pPublishURI = String.Empty;
		public string PublishURI
		{
			get
			{
				if (!String.IsNullOrEmpty(_pPublishURI))
				{
					return _pPublishURI;
				}

				_pPublishURI = base[publishUri] as String;

				return _pPublishURI;
			}
			set
			{
				_pPublishURI = value;
			}
		}

		public void SetPublishURI(string publishURI)
		{
			_pPublishURI = publishURI;
		}
	}
}
